package com.example.administrator.taxicar.ui.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.taxicar.R;
import com.example.administrator.taxicar.utils.CircleImageView;
import com.example.administrator.taxicar.utils.Constants;


public class MenuFragment extends Fragment {
    private LinearLayout xingcheng;
    private LinearLayout qianbao;
    private LinearLayout xiaoxi;
    private LinearLayout kefu;
    private LinearLayout shezhi;
    private LinearLayout fankui;
    private LinearLayout pingjia;
    private CircleImageView touxiang;
    private TextView login_text;

    private OnFragmentInterfaceListener mlistener;

    public static MenuFragment newInstance() {
        MenuFragment menuFragment = new MenuFragment();
        return menuFragment;
    }

    //加载fragment时调用
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Log.e("tag", "onAttach");
        if (context instanceof OnFragmentInterfaceListener) {
            mlistener = (OnFragmentInterfaceListener) context;
        } else {
            throw new RuntimeException(context.toString() + " must implement OnMenuFragmentInteractionListener");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_menu_layout, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        xingcheng = (LinearLayout) getView().findViewById(R.id.xingcheng_layout);
        qianbao = (LinearLayout) getView().findViewById(R.id.qianbao_layout);
        xiaoxi = (LinearLayout) getView().findViewById(R.id.xiaoxi_layout);
        kefu = (LinearLayout) getView().findViewById(R.id.kefu_layout);
        shezhi = (LinearLayout) getView().findViewById(R.id.shezhi_layout);
        fankui = (LinearLayout) getView().findViewById(R.id.fankui_layout);
        pingjia = (LinearLayout) getView().findViewById(R.id.pingjia_layout);
        touxiang = (CircleImageView) getView().findViewById(R.id.touxiang);
        login_text= (TextView) getView().findViewById(R.id.login_text);

        xingcheng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mlistener.toXingcheng();
            }
        });

        qianbao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mlistener.toQianbao();
            }
        });

        xiaoxi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mlistener.toXiaoxi();
            }
        });

        kefu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mlistener.toKefu();
            }
        });

        shezhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mlistener.toShezhi();
            }
        });

        fankui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mlistener.toFankui();
            }
        });

        touxiang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mlistener.toTouxiang();
            }
        });

        pingjia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mlistener.toPingjia();
            }
        });

        login_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mlistener.toLogin();
            }
        });
    }

    public void setTouxiangTupian(String path) {
        Bitmap b = BitmapFactory.decodeFile(path);
        if (b != null) {
            touxiang.setImageBitmap(b);
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("tag", "onCreate");
    }

    @Override
    public void onResume() {
        super.onResume();
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("feil_picture", Context.MODE_PRIVATE);
        String path = sharedPreferences.getString("path", "");
        Log.e("tag", "path:" + path);
        if (path != null) {
            setTouxiangTupian(path);
        }
        SharedPreferences sharedPreferences2 = getActivity().getSharedPreferences(Constants.LOGIN, Context.MODE_PRIVATE);
        String phone = sharedPreferences2.getString(Constants.USER_LOGIN,"");
        Log.e("tag", "phone:" + phone);
        if (!phone.equals("")) {
            login_text.setText(phone);
        }
    }

    public interface OnFragmentInterfaceListener {
        void toXingcheng();

        void toQianbao();

        void toXiaoxi();

        void toKefu();

        void toShezhi();

        void toFankui();

        void toTouxiang();

        void toLogin();

        void toPingjia();
    }
}
